package com.starblink.utils.ext.function

import android.graphics.Bitmap
import android.graphics.Rect
import android.graphics.RectF
import android.graphics.drawable.RotateDrawable
import android.util.Log
import android.view.MotionEvent
import java.lang.Integer.max
import kotlin.math.min

val RectF.hasValue get() = left > 0 || top > 0 || right > 0 || bottom > 0

fun RectF.reset() {
    left = 0f
    top = 0f
    right = 0f
    bottom = 0f
}

fun RectF.scale(scale: Float) {
    left *= scale
    top *= scale
    right *= scale
    bottom *= scale
}

fun RectF.scaleOther(scale: Float, other: RectF) {
    left = other.left * scale
    right = other.right * scale
    top = other.top * scale
    bottom = other.bottom * scale
}

fun RectF.overOther(other: RectF) =
    left <= other.left && top <= other.top && right >= other.right && bottom >= other.bottom


fun Rect.reset() {
    left = 0
    top = 0
    right = 0
    bottom = 0
}

fun Rect.scale(scale: Float) {
    left = (left * scale).toInt()
    top = (top * scale).toInt()
    right = (right * scale).toInt()
    bottom = (bottom * scale).toInt()
}

fun Rect.inOther(other: Rect, borderOffset: Int = 0): Boolean =
    this.left > other.left - borderOffset && this.right < other.right + borderOffset
            && this.top > other.top - borderOffset && this.bottom < other.bottom + borderOffset

fun RectF.inOther(other: RectF, borderOffset: Float = 0f) =
    this.left > other.left - borderOffset && this.right < other.right + borderOffset
            && this.top > other.top - borderOffset && this.bottom < other.bottom + borderOffset

fun MotionEvent.inRect(rect: RectF, borderOffset: Float = 0f): Boolean {
    return x > rect.left - borderOffset && x < rect.right + borderOffset
            && y > rect.top - borderOffset && y < rect.bottom + borderOffset
}

fun MotionEvent.inRect(rect: Rect, borderOffset: Int = 0): Boolean =
    x > rect.left - borderOffset && x < rect.right + borderOffset
            && y > rect.top - borderOffset && y < rect.bottom + borderOffset


fun RotateDrawable.rotate180() {
    level = 10000 / 2
}

fun RotateDrawable.rotate0() {
    level = 0
}


fun Bitmap.destroy() {
    try {
        if (!isRecycled) {
            recycle()
        }
    } catch (throwable: Throwable) {
        throwable.printStackTrace()
    }
}

fun Bitmap.createBitmap(x: Int, y: Int, width: Int, height: Int): Bitmap {
    if (isRecycled) {
        return Bitmap.createBitmap(1, 1, Bitmap.Config.RGB_565)
    }

    // 错误的x, y, width, height值，会引发异常，进行修正
    var finalX = if (x < 0 || x >= this.width) 0 else x
    var finalY = if (y < 0 || y >= this.height) 0 else y
    var finalWidth =
        if (x + width > this.width) this.width - x else max(min(width, this.width), 1)
    var finalHeight =
        if (y + height > this.height) this.height - y else max(min(height, this.height), 1)
    var bitmap: Bitmap
    try {
        //!!!注意IllegalArgumentException:if the x, y, width, height values are outside of the dimensions of the source bitmap, or width is <= 0, or height is <= 0
        bitmap = Bitmap.createBitmap(
            this,
            finalX,
            finalY,
            finalWidth,
            finalHeight
        )
    } catch (e: IllegalArgumentException) {
        Log.e("Bitmap", "createBitmap失败,[finalX, finalY, finalWidth,finalHeight]=[${finalX},${finalY},${finalWidth},${finalHeight}]", e)
        bitmap = Bitmap.createBitmap(1, 1, Bitmap.Config.RGB_565)
    }
    return bitmap
}